/**
 * moco 
 */
$.extend({
	currentTimeMillis : function(){
		return new Date().getTime();
	}
	
});
$.extend({
	saveData : function(formId ,currControlData){
		var formData = $('#'+ formId).serializeArray();
		for (var i = 0; i < formData.length; i++) {
			var name 	= formData[i].name;
			var value 	= formData[i].value;
			for (var j = 0; j < formData.length; j++) {
				if(currControlData[j].id == name){
					currControlData[j].value = value;
				}
			}
		}
		return true;
	},
	submitHappyTripObject : function(formData ,url ,type){
		$.showProgressStart();
		$.ajax({
			url : url,
			//type : 'get',
			data: $.createFormData(formData),
			dataType : 'jsonp',
			success : function(data){
				console.log(data);
				$('#exeResult').empty();
				$('#exeResult').text(JSON.stringify(data));
				if ('estimatePrice'===type&&data.content&&data.content.priceItems&&data.content.priceItems[0].dynamicMD5){
					$.control.createOrder[26].value = data.content.priceItems[0].dynamicMD5;
				} else if ('createOrder'===type&&data.content&&data.content.id){
					$.control.cancelOrder[0].value    = data.content.id;
					$.control.complaintOrder[0].value = data.content.id;
					$.control.closeTip[0].value       = data.content.id;
					$.control.feeConfirm[0].value 	  = data.content.id;
					$.global.order_id = data.content.id;
					$.getOrderId(data.content.id);
				}
				if (''!= $.global.order_id ){
					$.getOrderDetail($.global.order_id);
				}
				$.showProgressEnd(true);
				$('#exeResult').css('color','#5cb85c');
			},
			error : function(data){
				console.info(data);
				$.showProgressEnd(false);
				$('#exeResult').css('color','#d9534f');
			}
		});
	},
	//getOrderInfo
	getOrderDetail : function(orderId){
		$.ajax({
			url : $.happytripBaseUrl + 'taxi/special/getOrderInfo.jsonp?id='+orderId,
			dataType : 'jsonp',
			success : function(data){
				console.log(data);
				$('#exeOrderInfo').empty();
				$('#exeOrderInfo').text(JSON.stringify(data));
			},
			error : function(data){
				
			}
		});
	},
	getOrderId :function(id){
		$.ajax({
			url : '/gethappytripId?id='+id,
			dataType : 'json',
			success : function(data){
				$('#happytripId').text('['+id+']');
				$('#didiId').text('['+data.order_id+']');
				$.moco.acceptOrder[0].value    = data.order_id;
				$.moco.driverConfirmReach[0].value    = data.order_id;
				$.moco.driverConfirmTaken[0].value    = data.order_id;
				$.moco.driverArrive[0].value    = data.order_id;
				$.moco.driverCancelOrder[0].value    = data.order_id;
				$.moco.autoCallback[0].value    = data.order_id;
				$.moco.modifyFee[0].value    = data.order_id;
				$.moco.feeRefund[0].value    = data.order_id;
				$.moco.setException[0].value    = data.order_id;
				$.moco.closeOrder[0].value    = data.order_id;
			},
			error : function(data){
				
			}
		});
	},
	submitMocoObject : function(formData ,url ,type){
		$.showProgressStart();
		$.ajax({
			url : url,
			//type : 'get',
			data: $.createFormData(formData),
			dataType : 'json',
			success : function(data){
				console.info(data);
				$('#exeResult').empty();
				$('#exeResult').text(JSON.stringify(data));
				if (data.success&&'true'===data.success){
					$.showProgressEnd(true);
					$('#exeResult').css('color','#5cb85c');
				}else{
					$.showProgressEnd(false);
					$('#exeResult').css('color','#d9534f');
				}
				if (''!= $.global.order_id ){
					$.getOrderDetail($.global.order_id);
				}
			},
			error : function(data){
				console.info(data);
				$.showProgressEnd(false);
				$('#exeResult').css('color','#d9534f');
			}
		});
	},
    createFormData : function(formData){
        var data = "";
        for (var i=0; i<formData.length; i++){
            if ( i+1 == formData.length)
                data += formData[i].id + '=' + formData[i].value + '';
            else
                data += formData[i].id + '=' + formData[i].value + '&';
        }
        return data;
    },
	/**
	 * 提交表单
	 * @param  {[type]}
	 * @param  {[type]}
	 * @return {[type]}
	 */
	submitForm : function(fromName , url){
		$.ajax({
			url : url,
			type : 'post',
			dataType : 'text',
			data : $('#'+fromName).serialize(),
			success : function(data){
				console.info(data.data);
			},
			error : function(data){
				console.info(data.data);
			}
		});
	},
	/**
	 * 弹出模态框
	 * @param  {String} form 嵌入位置ID
	 * @param  {String} 标题标签ID
	 * @param  {String} 表单初始化数据
	 * @param  {String} 标题
	 */
	openFrom : function(mainId, titleId, controlData, title, url){
		$.global.url = url;
		$.currControlData = controlData;
		var form = $.createForm(mainId, controlData);
		$('#'+titleId).text(title);
		$('#'+mainId).empty();
		$('#'+mainId).append(form);
	},
	/**
	 * @param  {String} formName 表单name and id
	 * @param  {Object} controlData 表单结构数据
	 * @return {Object} 返回表单对象
	 */
	createForm : function(formName, controlData){
		var formControl = $.createDocumentTag('<form></form>','form-horizontal', 'dataform');
		formControl.attr('role','form');
		for (var i = 0; i<controlData.length; i++) {
			var control =  $.createInputControl(
				controlData[i].id,
				controlData[i].label,
				controlData[i].type,
				controlData[i].col,
				null,
				controlData[i].value);
			control.appendTo(formControl);
		}
		return formControl;
	},
	/**
	 * 创建一个input
	 * @param  {String} inputId name 属性
	 * @param  {String} inputLabel 提示文字
	 * @param  {String} inputType 类型 'text'
	 * @param  {String} inputCol 宽度 1~10
	 * @param  {String} placeholder 内嵌提示文字
	 * @return {String}
	 */
	createInputControl : function(inputId, inputLabel, inputType, inputCol, placeholder, value){
		if (!placeholder) placeholder = inputLabel;
		var control 		= $.createDocumentTag('<div></div>'		,'form-group');
		var controlLabel 	= $.createDocumentTag('<label>'+inputLabel+'</label>'	,'col-sm-3 control-label',null,null,inputId);
		var controlGroup 	= $.createDocumentTag('<div></div>'		,'col-sm-'+inputCol);
		var controlInput 	= $.createDocumentTag('<input></input>'	,'form-control', inputId, placeholder ,null ,inputType);
		controlInput.attr('value',value);
		controlInput.appendTo(controlGroup);
		controlLabel.appendTo(control);
		controlGroup.appendTo(control);

		return control;
	},
	/**
	 * 创建html标签
	 * @param  {String} tag
	 * @param  {String} className
	 * @param  {String} id
	 * @param  {String} placeholder
	 * @param  {String} forName
	 * @param  {String} type
	 * @return {String}
	 */
	createDocumentTag : function(tag ,className ,id ,placeholder ,forName, type){
		var tagObject = $(tag);
		if (type) tagObject.attr('type',type);

		if (id && '<input></input>'==tag) tagObject.attr('name',id);

		if (id) 	   		tagObject.attr('id',id);
		//else 				tagObject.attr('id', $.currentTimeMillis());
		if (className) 		tagObject.addClass(className);
		if (placeholder) 	tagObject.attr('placeholder',placeholder);
		if (forName) 		tagObject.attr('for',forName);

		return tagObject;
	},
	/**
	 * [showProgress description]
	 * @param  {Boolean} isSuccess [description]
	 * @return {[type]}            [description]
	 */
	showProgressStart :function(){
		$('#exeProgress').removeClass("progress-bar-success");
		$('#exeProgress').removeClass("progress-bar-danger");
		$('#exeProgress').addClass("progress-bar-info");
		$('#exeProgress').width('10%');
	},
	showProgressEnd :function(isSuccess){
		$('#exeProgress').width('100%');	
		if(isSuccess){//成功
			$('#exeProgress').removeClass("progress-bar-info");
			$('#exeProgress').removeClass("progress-bar-danger");
			$('#exeProgress').addClass("progress-bar-success");
		}else{//失败
			$('#exeProgress').removeClass("progress-bar-info");
			$('#exeProgress').removeClass("progress-bar-success");;
			$('#exeProgress').addClass("progress-bar-danger");
		}
	}
});